//
//  AppDelegate.swift
//  weibo
//
//  Created by David on 15/11/13.
//  Copyright © 2015年 DV. All rights reserved.
//

import UIKit
let DVSwitchMainInterfaceNotification = "DVSwitchMainInterfaceNotification"
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        setupAppearance()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
//        window?.backgroundColor = UIColor.orangeColor()
        window?.rootViewController = defaultView()

        window?.makeKeyAndVisible()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "SwitchMainInterface:", name: DVSwitchMainInterfaceNotification, object: nil)
        return true
    }
    @objc private func SwitchMainInterface(notif : NSNotification){
        
       window?.rootViewController =  notif.object != nil ? mainViewController() : welcomeViewController()
        
    }
    private func defaultView()->UIViewController{
        //判断是否登入
        if UserAccount.loadAccount() != nil{
            return isNewVersion() ? newFeatureViewController() : welcomeViewController()
        }
        //判断是否新版本
        return mainViewController()
        //默认
        
    }
    private func isNewVersion()->Bool{
        let sandBox = "sandBoxVersion"
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandBox)
        
       let curruntVersion = Double( NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        //保存现在版本
        NSUserDefaults.standardUserDefaults().setDouble(curruntVersion, forKey: sandBox)
        return curruntVersion > sandBoxVersion
    }
    private func setupAppearance(){
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }
   

}

